package com.bw.base.mvvm

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.DatePicker
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType
import java.util.Calendar

abstract class BaseActivity<VDB:ViewDataBinding,VM:BaseViewModel>:AppCompatActivity() {
    private val type by lazy {
        //取出泛型参数列表
        (javaClass.genericSuperclass as ParameterizedType) .actualTypeArguments
    }
    protected val binding by lazy {
        var clazz=type[0] as Class<VDB>
        var method=clazz.getMethod("inflate",LayoutInflater::class.java)
        method.invoke(null,layoutInflater) as VDB
    }
    protected val viewmodel by lazy {
        val clazz=type[1] as Class<VM>
        ViewModelProvider(this)[clazz]
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        intitView()
        intitData()
    }

    abstract fun intitView()

    abstract fun intitData()

    fun showDate(ed:EditText){
       val calendar=Calendar.getInstance()
        DatePickerDialog(this,DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
            val mon=if (month+1<10) "0"+(month+1) else month+1
            val day=if (dayOfMonth<10) "0"+dayOfMonth else dayOfMonth
            ed.setText(""+year +"-"+mon+"-"+day)
        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show()
    }
    fun showTime(ed:EditText){
        val calendar=Calendar.getInstance()
        TimePickerDialog(this,TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
            val hour=if (hourOfDay<10) "0"+hourOfDay else hourOfDay
            val min=if (minute<10) "0"+minute else minute
            ed.setText(""+hour+":"+min)
        },calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),true).show()
    }
}